<template>
  <div class="container">
    <div class="app-container">
      <el-tree
        v-loading="isLoading"
        :data="depts"
        :expand-on-click-node="false"
        :props="defaultProps"
        default-expand-all
      >
        <!--  节点结构  -->
        <!-- v-slot = "{ node, data }" 只能作用在template上 -->
        <template v-slot="{ data }">
          <el-col style="display: flex;justify-content: space-between;">
            <el-row>{{ data.name }}</el-row>
            <el-row>
              <span style="margin-right: 20px;">{{
                data.managerName.length === 2 ? data.managerName.charAt(0) + '&nbsp;&nbsp;&nbsp;' + data.managerName.charAt(1) : data.managerName
              }}</span>
              <el-dropdown style="margin-right: 10px;" trigger="click" @command="handleCommand($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="default">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="delete">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-row>
          </el-col>
        </template>
      </el-tree>
    </div>
    <AddDept ref="handleDeptRef" @updateDepartment="getDepartmentList" />
  </div>
</template>

<script>
import { departmentListService } from '@/api/department'
import { transListToTreeData } from '@/utils'
import AddDept from '@/views/department/components/add-dept.vue'

export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      isLoading: false, // 控制加载状态
      depts: [], // 部门列表
      defaultProps: {
        children: 'children', // 指定 children 属性的名字
        label: 'name' // 指定 label 属性的名字
      }
    }
  },
  created() {
    // 获取部门列表
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      this.isLoading = true
      const result = await departmentListService()
      // 处理部门名称
      result.forEach(item => {
        if (item.managerName.includes('黑马')) {
          item.managerName = item.managerName.substring(2)
        }
      })
      // 转换成树形结构
      this.depts = transListToTreeData(result, 0)
      this.isLoading = false
    },
    handleCommand(type, id) {
      if (type === 'add' || type === 'edit') {
        this.$refs.handleDeptRef.showDialog(id, type)
      }
      if (type === 'delete') {
        this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          // 删除部门
          await this.$refs.handleDeptRef.handleDelete(id)
          // 提示信息
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          // 刷新列表
          await this.getDepartmentList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 10px 20px;
  font-size: 14px;

}

.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}

.el-icon-arrow-down {
  font-size: 12px;
}
</style>

